from datetime import datetime
from flask import current_app
from werkzeug.utils import secure_filename
import os


def save_uploaded_file(file):
    """
    保存上传文件，保证文件名唯一
    """
    filename = secure_filename(file.filename)
    # 添加时间戳保证唯一
    timestamp = int(datetime.now().timestamp())  # ✅ 修复点
    unique_filename = f"{timestamp}_{filename}"
    filepath = os.path.join(current_app.config['UPLOAD_FOLDER'], unique_filename)
    file.save(filepath)
    return filepath


def allowed_file(filename):
    """检查文件扩展名是否允许上传"""
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS']
